package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.bean.RoomEnvironmentalTypeBean;
import com.qiantu.phone.ui.activity.AirQualityActivity;
import com.qiantu.phone.ui.activity.HumanBodyInductionActivity;
import com.qiantu.phone.ui.activity.HumanPresenceInductionActivity;
import com.qiantu.phone.ui.activity.HumidityActivity;
import com.qiantu.phone.ui.activity.NoiseActivity;
import com.qiantu.phone.ui.activity.TemperatureActivity;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/09/22
 * desc   : 房间标签数据列表
 */
public final class RoomEnvironmentalTypeAdapter extends AppAdapter<RoomEnvironmentalTypeBean> {

    public RoomEnvironmentalTypeAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvFloorRoom;
        private final RecyclerView deviceRecyclerView;

        private ViewHolder() {
            super(R.layout.item_room_environmental_type);
            tvFloorRoom = findViewById(R.id.tv_floor_room);
            deviceRecyclerView = findViewById(R.id.device_recycler_view);
        }

        @Override
        public void onBindView(int position) {
            RoomEnvironmentalTypeBean roomEnvironmentalType = getItem(position);
            tvFloorRoom.setText(roomEnvironmentalType.getFloorRoomName());
            EnvironmentalTypeAdapter environmentalTypeAdapter = (EnvironmentalTypeAdapter) deviceRecyclerView.getAdapter();
            if (environmentalTypeAdapter == null) {
                environmentalTypeAdapter = new EnvironmentalTypeAdapter(getContext());
                deviceRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            }
            environmentalTypeAdapter.setData(roomEnvironmentalType.getEnvironmentalTypes());
            environmentalTypeAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    RoomEnvironmentalTypeBean.EnvironmentalType environmentalType = ((EnvironmentalTypeAdapter) recyclerView.getAdapter()).getData().get(position);
                    Intent intent = null;
                    switch (environmentalType.getType()) {
                        case DeviceBean.ENVIRONMENT_TYPE_TEMPERATURE:
                            intent = new Intent(getContext(), TemperatureActivity.class);
                            break;
                        case DeviceBean.ENVIRONMENT_TYPE_HUMIDITY:
                            intent = new Intent(getContext(), HumidityActivity.class);
                            break;
                        case DeviceBean.ENVIRONMENT_TYPE_AIR_QUALITY:
                            intent = new Intent(getContext(), AirQualityActivity.class);
                            break;
                        case DeviceBean.ENVIRONMENT_TYPE_HUMAN_BODY_INDUCTION:
                            intent = new Intent(getContext(), HumanBodyInductionActivity.class);
                            break;
                        case DeviceBean.ENVIRONMENT_TYPE_HUMAN_PRESENCE_INDUCTION:
                        case 16:
                        case 17:
                        case 18:
                        case 19:
                        case 20:
                        case 21:
                        case 22:
                        case 23:
                        case 24:
                            intent = new Intent(getContext(), HumanPresenceInductionActivity.class);
                            intent.putExtra("environmentalType",environmentalType.getType());
                            break;
                        case 12:
                        case 13:
                        case 14:
                            intent = new Intent(getContext(), NoiseActivity.class);
                            break;
                    }
                    if (intent != null) {
                        intent.putExtra("environmentalType",environmentalType.getType());
                        intent.putExtra("is_indoor", true);
                        intent.putExtra("floor_room_name", roomEnvironmentalType.getFloorRoomName());
                        intent.putExtra("room_serial_no", roomEnvironmentalType.getRoomSerialNo());
                        intent.putExtra("device_serial_no", environmentalType.getDeviceSerialNo());
                        intent.putExtra("device_name", environmentalType.getDeviceName());
                        getContext().startActivity(intent);
                    }
                }
            });
            deviceRecyclerView.setAdapter(environmentalTypeAdapter);
        }
    }
}